<template>
  <div class="upload-container">
    <Header title="上传管理">
      <template #right>
        <van-button
          type="primary"
          size="small"
          @click="selectFiles"
        >
          上传
        </van-button>
      </template>
    </Header>

    <div class="upload-content">
      <!-- 隐藏的文件选择器 -->
      <input
        ref="fileInputRef"
        type="file"
        multiple
        style="display: none"
        @change="handleFileSelect"
      />

      <!-- 总进度条 -->
      <div v-if="uploadTasks.length > 0" class="total-progress">
        <div class="progress-header">
          <span class="progress-title">总体进度</span>
          <span class="progress-percentage">{{ totalProgress }}%</span>
        </div>
        <van-progress
          :percentage="totalProgress"
          :color="isUploading ? '#1989fa' : '#07c160'"
          stroke-width="6"
          :show-pivot="false"
        />
      </div>

      <!-- 上传任务列表 -->
      <div class="task-list">
        <div class="task-header">
          <h3>上传任务 ({{ uploadTasks.length }})</h3>
          <div class="task-actions">
            <van-button
              v-if="completedTasks.length > 0"
              size="small"
              icon="clear"
              @click="clearCompletedTasks"
            >
              清除已完成
            </van-button>
            <van-button
              v-if="uploadTasks.length > 0"
              size="small"
              type="danger"
              icon="delete-o"
              @click="clearAllTasks"
            >
              清空列表
            </van-button>
          </div>
        </div>

        <div v-if="uploadTasks.length === 0" class="empty-state">
          <van-empty description="暂无上传任务" />
        </div>

        <div
          v-for="task in uploadTasks"
          :key="task.id"
          class="task-item"
          :class="{ active: task.status === 'uploading' }"
        >
          <div class="task-icon">
            <van-icon
              :name="getTaskIcon(task)"
              :color="getTaskIconColor(task)"
              size="20"
            />
          </div>

          <div class="task-info">
            <div class="task-name">{{ task.fileName }}</div>
            <div class="task-meta">
              <span>{{ formatFileSize(task.fileSize) }}</span>
              <span v-if="task.status === UploadStatus.UPLOADING">
                {{ formatSpeed(task.speed) }}
              </span>
              <span v-if="task.status === UploadStatus.UPLOADING && task.remainingTime > 0">
                剩余 {{ formatTimeRemaining(task.remainingTime) }}
              </span>
            </div>

            <!-- 进度条 -->
            <div v-if="task.status !== UploadStatus.WAITING" class="task-progress">
              <van-progress
                :percentage="task.progress"
                :color="getProgressColor(task.status)"
                :show-pivot="false"
                stroke-width="4"
              />
              <span class="progress-text">{{ task.progress }}%</span>
            </div>
          </div>

          <div class="task-actions">
            <van-button
              v-if="task.status === UploadStatus.UPLOADING"
              size="mini"
              icon="pause"
              @click="pauseTask(task.id)"
            >
              暂停
            </van-button>
            <van-button
              v-else-if="task.status === UploadStatus.PAUSED"
              size="mini"
              type="primary"
              icon="play"
              @click="resumeTask(task.id)"
            >
              继续
            </van-button>
            <van-button
              v-else-if="task.status === UploadStatus.ERROR"
              size="mini"
              type="primary"
              icon="replay"
              @click="retryTask(task.id)"
            >
              重试
            </van-button>
            <van-button
              size="mini"
              type="danger"
              icon="delete"
              @click="removeTask(task.id)"
            >
              删除
            </van-button>
          </div>
        </div>
      </div>

      <!-- 统计信息 -->
      <div v-if="uploadTasks.length > 0" class="upload-stats">
        <div class="stat-item">
          <span class="stat-label">总任务：</span>
          <span class="stat-value">{{ uploadTasks.length }}</span>
        </div>
        <div class="stat-item">
          <span class="stat-label">已完成：</span>
          <span class="stat-value">{{ completedTasks.length }}</span>
        </div>
        <div class="stat-item">
          <span class="stat-label">上传中：</span>
          <span class="stat-value">{{ uploadingTasks.length }}</span>
        </div>
        <div class="stat-item">
          <span class="stat-label">失败：</span>
          <span class="stat-value">{{ errorTasks.length }}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { showToast, showConfirmDialog } from 'vant'
import Header from '@/components/Header.vue'
import { useUploadStore, UploadStatus } from '@/stores/upload'
import { useFileStore } from '@/stores/file'

const uploadStore = useUploadStore()
const fileStore = useFileStore()

const {
  tasks: uploadTasks,
  uploadingTasks,
  completedTasks,
  successTasks,
  errorTasks,
  pausedTasks,
  totalProgress,
  isUploading,
  addUploadTasks,
  pauseTask,
  resumeTask,
  removeTask,
  retryTask,
  clearCompletedTasks,
  clearAllTasks,
  pauseAll,
  resumeAll,
  formatFileSize,
  formatSpeed,
  formatTimeRemaining
} = uploadStore

const { currentFolderId } = fileStore

const fileInputRef = ref<HTMLInputElement>()

// 方法
const selectFiles = () => {
  fileInputRef.value?.click()
}

const handleFileSelect = (event: Event) => {
  const target = event.target as HTMLInputElement
  const files = target.files

  if (files && files.length > 0) {
    addUploadTasks(files, currentFolderId)
    showToast({
      type: 'success',
      message: `已添加 ${files.length} 个文件到上传队列`
    })
  }

  // 清空文件输入框
  target.value = ''
}

const getTaskIcon = (task: any) => {
  switch (task.status) {
    case UploadStatus.WAITING:
      return 'clock-o'
    case UploadStatus.UPLOADING:
      return 'upgrade'
    case UploadStatus.SUCCESS:
      return 'success'
    case UploadStatus.ERROR:
      return 'cross'
    case UploadStatus.PAUSED:
      return 'pause'
    case UploadStatus.CANCELED:
      return 'cross'
    default:
      return 'description'
  }
}

const getTaskIconColor = (task: any) => {
  switch (task.status) {
    case UploadStatus.WAITING:
      return '#969799'
    case UploadStatus.UPLOADING:
      return '#1989fa'
    case UploadStatus.SUCCESS:
      return '#07c160'
    case UploadStatus.ERROR:
      return '#ee0a24'
    case UploadStatus.PAUSED:
      return '#ff976a'
    case UploadStatus.CANCELED:
      return '#969799'
    default:
      return '#969799'
  }
}

const getProgressColor = (status: string) => {
  switch (status) {
    case UploadStatus.UPLOADING:
      return '#1989fa'
    case UploadStatus.SUCCESS:
      return '#07c160'
    case UploadStatus.ERROR:
      return '#ee0a24'
    case UploadStatus.PAUSED:
      return '#ff976a'
    case UploadStatus.CANCELED:
      return '#969799'
    default:
      return '#1989fa'
  }
}
</script>

<style scoped>
.upload-container {
  height: 100vh;
  display: flex;
  flex-direction: column;
  background-color: #f8f8f8;
}

.upload-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.total-progress {
  margin: 0 16px 16px;
  padding: 16px;
  background: white;
  border-radius: 12px;
}

.progress-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}

.progress-title {
  font-size: 16px;
  font-weight: 500;
  color: #323233;
}

.progress-percentage {
  font-size: 14px;
  color: #1989fa;
  font-weight: 500;
}

.task-list {
  flex: 1;
  margin: 0 16px 16px;
  background: white;
  border-radius: 12px;
  overflow: hidden;
}

.task-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px;
  border-bottom: 1px solid #f5f5f5;
}

.task-header h3 {
  margin: 0;
  font-size: 16px;
  color: #323233;
}

.task-actions {
  display: flex;
  gap: 8px;
}

.empty-state {
  padding: 40px 20px;
}

.task-item {
  display: flex;
  align-items: center;
  padding: 16px;
  border-bottom: 1px solid #f5f5f5;
  transition: background-color 0.2s;
}

.task-item.active {
  background-color: #f7f8fa;
}

.task-item:last-child {
  border-bottom: none;
}

.task-icon {
  margin-right: 12px;
  flex-shrink: 0;
}

.task-info {
  flex: 1;
  min-width: 0;
}

.task-name {
  font-size: 14px;
  color: #323233;
  margin-bottom: 4px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.task-meta {
  font-size: 12px;
  color: #969799;
  display: flex;
  gap: 8px;
  margin-bottom: 8px;
}

.task-progress {
  display: flex;
  align-items: center;
  gap: 8px;
}

.task-progress :deep(.van-progress) {
  flex: 1;
}

.progress-text {
  font-size: 12px;
  color: #969799;
  min-width: 40px;
}

.task-actions {
  display: flex;
  gap: 4px;
  margin-left: 12px;
}

.upload-stats {
  margin: 0 16px 16px;
  padding: 16px;
  background: white;
  border-radius: 12px;
  display: flex;
  justify-content: space-around;
  margin-bottom: 60px;
}

.stat-item {
  text-align: center;
}

.stat-label {
  display: block;
  font-size: 12px;
  color: #969799;
  margin-bottom: 4px;
}

.stat-value {
  display: block;
  font-size: 16px;
  font-weight: 500;
  color: #323233;
}
</style>